﻿using UnityEngine;
using System.Collections;

namespace Mogo.Util
{
	public class EventBase<T>
		where T:EventBase<T>,new()
	{
		private static T selfs;
		private static bool inited = false;
		protected EventBase() { }
		/// <summary>
		/// 直接调用添加监听者
		/// </summary>
		/// <param name="action"></param>
		public static void AddEventListener(System.Action<T> action)
		{
			EventDispatcher.AddEventListener<T>(typeof(T).ToString(),action);
		}
		public static string EventName
		{
			get { return typeof(T).ToString(); }
		}
		/// <summary>
		/// 直接调用移除监听者
		/// </summary>
		/// <param name="action"></param>
		public static void RemoveEventListener(System.Action<T> action)
		{
			EventDispatcher.RemoveEventListener<T>(EventName,action);
		}
		public static void MarkAsPermanent()
		{
			EventDispatcher.MarkAsPermanent(EventName.ToString());
		}
		/// <summary>
		/// 广播事件
		/// </summary>
		/// <param name="args"></param>
		public static void Trigger(T args,bool isLog = true)
		{
			EventDispatcher.TriggerEvent<T>(EventName,args,isLog);
		}
		public void Trigger(bool isLog = true)
		{
			EventDispatcher.TriggerEvent<T>(EventName,this as T,isLog);
		}
		/// <summary>
		/// 创建一个实例
		/// </summary>
		/// <returns></returns>
		public static T Build()
		{
			if(selfs == null)
			{
				selfs = new T();
			}
			return selfs;
		}
	}
}